package com.ljb.lib_http;

import java.lang.reflect.Type;

/**
 * HTTPLoader的策略加载类
 * Created by ljb on 2018/6/21.
 */
public class HttpLoaderStratergy implements HttpLoader {
    private static HttpLoaderStratergy stratergy;
    private HttpLoader httpLoader;

    private HttpLoaderStratergy() {
    }

    /**
     * 获取策略加载类
     *
     * @return
     */
    public static HttpLoaderStratergy getLoader() {
        if (stratergy == null) {
            synchronized (HttpLoaderStratergy.class) {
                if (stratergy == null) {
                    stratergy = new HttpLoaderStratergy();
                }
            }
        }
        return stratergy;
    }

    /**
     * 设置网络请求策略
     *
     * @param httpLoader
     */
    public void setStratergy(HttpLoader httpLoader) {
        this.httpLoader = httpLoader;
    }

    /**
     * @param url          网络请求地址
     * @param httpParams   请求参数,键值对形式的参数
     * @param tag          网络请求标记,方便取消请求用的
     * @param clazz        返回数据解析Class,请参考gson解析
     * @param httpCallback 网络请求回调
     * @param <T>
     */
    @Override
    public <T> void get(String url, HttpParams httpParams, Object tag, Class<T> clazz, HttpCallback<T> httpCallback) {
        if (httpLoader != null) {
            httpLoader.get(url, httpParams, tag, clazz, httpCallback);
        } else {
            throw new NullPointerException("httpLoader is null, setStratergy first");
        }
    }

    /**
     * @param url          网络请求地址
     * @param httpParams   请求参数,键值对形式的参数
     * @param tag          网络请求标记,方便取消请求用的
     * @param type         返回数据解析Type,请参考gson解析
     * @param httpCallback 网络请求回调
     * @param <T>
     */
    @Override
    public <T> void get(String url, HttpParams httpParams, Object tag, Type type, HttpCallback<T> httpCallback) {
        if (httpLoader != null) {
            httpLoader.get(url, httpParams, tag, type, httpCallback);
        } else {
            throw new NullPointerException("httpLoader is null, setStratergy first");
        }
    }

    /**
     * @param url          网络请求地址
     * @param httpParams   请求参数,键值对形式的参数
     * @param tag          网络请求标记,方便取消请求用的
     * @param clazz         返回数据解析Class,请参考gson解析
     * @param httpCallback 网络请求回调
     * @param <T> 泛型
     */
    @Override
    public <T> void post(String url, HttpParams httpParams, Object tag, Class<T> clazz, HttpCallback<T> httpCallback) {
        if (httpLoader != null) {
            httpLoader.post(url, httpParams, tag, clazz, httpCallback);
        } else {
            throw new NullPointerException("httpLoader is null, setStratergy first");
        }
    }

    /**
     * @param url          网络请求地址
     * @param httpParams   请求参数,键值对形式的参数
     * @param tag          网络请求标记,方便取消请求用的
     * @param type         返回数据解析Type,请参考gson解析
     * @param httpCallback 网络请求回调
     * @param <T> 泛型
     */
    @Override
    public <T> void post(String url, HttpParams httpParams, Object tag, Type type, HttpCallback<T> httpCallback) {
        if (httpLoader != null) {
            httpLoader.post(url, httpParams, tag, type, httpCallback);
        } else {
            throw new NullPointerException("httpLoader is null, setStratergy first");
        }
    }

    /**
     * 根据tag取消网络请求
     *
     * @param tag
     */
    @Override
    public void stop(Object tag) {
        if (httpLoader != null) {
            httpLoader.stop(tag);
        } else {
            throw new NullPointerException("httpLoader is null, setStratergy first");
        }
    }

    /**
     * 取消所有网络请求
     */
    @Override
    public void stopAll() {
        if (httpLoader != null) {
            httpLoader.stopAll();
        } else {
            throw new NullPointerException("httpLoader is null, setStratergy first");
        }
    }
}
